﻿#region 命名空间

using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Linq;
using System.Collections.Generic;
using WinnerCMS.Data;
using WinnerCMS.Common;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.BLL
{
    public class MessageManage : MarshalByRefObject
    {
        private static readonly MessageDal Dal = new MessageDal();

        #region 发送短信

        /// <summary>
        /// 发送短信
        /// </summary>
        public Result Add(Message model)
        {
            Result res = new Result();
            model.SendTime = DateTime.Now;

            Dal.Add(model);


            return res;
        }

        #endregion

        #region 删除短信

        /// <summary>
        /// 删除短信
        /// </summary>
        /// 
        public void Delete(string IDs, UpdatePanel UpdatePanel1)
        {
            int[] Ints = MyConvert.ConvertToArr(IDs);
            Dal.Delete(Ints);
            MsgBox.Message("删除成功！", UpdatePanel1);
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="IDs"></param>
        public void LogicDeleteOrRestoration(string ID, UpdatePanel UpdatePanel1)
        {
            Dal.LogicDeleteOrRestoration(int.Parse(ID));
            MsgBox.Message("删除成功！", UpdatePanel1);
        }

        /// <summary>
        /// 清空用户的短信
        /// </summary>
        public void Del_ForUser()
        {
            Dal.DelForUser(new[] {UserManage.Current.UserName});
            HttpContext.Current.Response.Redirect("Message.aspx?Action=Inbox");
        }

        #endregion

        #region 绑定数据

        /// <summary>
        /// 绑定数据
        /// </summary>
        public IList<Message> BindList(string sort, string keyword, PagingParam pp)
        {
            return Dal.BindList(sort, keyword, pp);
        }

        #endregion

        #region 检查是否有了新短信

        /// <summary>
        /// 检查是否有了新短信
        /// </summary>
        public int CountNoRead()
        {
            int i = Dal.CountNoRead(UserManage.Current.UserName);
            return i;
        }

        #endregion

        #region 加载数据

        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="GUID"></param>
        /// <returns></returns>
        public Message GetInfo(string GUID)
        {
            return Dal.ReadMsg(GUID);
        }

        #endregion

        #region 批量执行操作--网站后台用的

        /// <summary>
        /// 批量执行操作--网站后台用的
        /// </summary>
        /// <param name="GV"></param>
        /// <param name="ddlBatchToDo"></param>
        public void BatchToDo(GridView GV, DropDownList ddlBatchToDo, UpdatePanel UpdatePanel1)
        {
            string users = "";

            switch (ddlBatchToDo.SelectedValue)
            {
                case "1": //批量删除短信
                    string IDs = GetSelectedRow.GetIDs(GV);
                    int[] Ints = (from x in IDs.Split(',') select int.Parse(x)).ToArray();
                    Dal.Delete(Ints);
                    MsgBox.Message("删除成功！", UpdatePanel1);
                    break;
                case "2": //删除选中用户所有短信
                    users = GetSelectedRow.GetIDs(GV, 2, "CB_SelectID");
                    string[] arrUsers = (from x in users.Split(',') select x).ToArray();
                    Dal.DelForUser(arrUsers);
                    MsgBox.Message("删除成功！", UpdatePanel1);
                    break;
                case "3": //向被选中的会员发短信
                    users = GetSelectedRow.GetIDs(GV, 3, "CB_SelectID");
                    HttpContext.Current.Response.Redirect("Message_Send.aspx?Action=SendToSomeBoby&Users=" + users);
                    break;
                case "4": //向所有会员发送短信
                    HttpContext.Current.Response.Redirect("Message_Send.aspx?Action=SystemSend");
                    break;
                default:
                    MsgBox.Message("参数错误", UpdatePanel1);
                    break;
            }
        }

        #endregion
    }
}